package liumengbaio.lolzhangyoubao.Activity.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;


import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import liumengbaio.lolzhangyoubao.Activity.Adapter.VideoAdapter;
import liumengbaio.lolzhangyoubao.Activity.Activity.VideoActivity;
import liumengbaio.lolzhangyoubao.Activity.InFo.VideoEntity;
import liumengbaio.lolzhangyoubao.R;

/**
 * 娱乐界面
 * Created by lanouhn on 16/8/23.
 */
public class FragmentVideo_recreation extends Fragment {

    private GridView mGridView;
    private VideoEntity mVideoEntity;
    private VideoAdapter mVideoAdapter;
    private String url = "http://lol.zhangyoubao.com/apis/rest/CatalogsService/all?c" +
            "attype=video&i_=EAC1B788-00BC-454A-A9B9-460852CFC011&t_=1438755336&" +
            "p_=18353&v_=40050303&d_=ios&osv_=8.3&version=0&a_=lol";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_video_recreation, null);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView(getView());
        initEvent();
    }

    private void initView(View view) {
        mGridView = (GridView) view.findViewById(R.id.video_gridview_recreation);
        getDataFromWeb();
    }

    private void initEvent() {
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), VideoActivity.class);
                intent.putExtra("name", mVideoEntity.getData().get(0).getCatword_id().get(position).getName());
                intent.putExtra("desc", mVideoEntity.getData().get(0).getCatword_id().get(position).getDesc());
                intent.putExtra("pic", mVideoEntity.getData().get(0).getCatword_id().get(position).getPic_url());
                intent.putExtra("id", mVideoEntity.getData().get(0).getCatword_id().get(position).getId());
                startActivity(intent);
            }
        });
    }


    private void getDataFromWeb() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder().url(url).build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Request request, IOException e) {
                        Toast.makeText(getActivity(), "失败了！", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(Response response) throws IOException {
                        String result = response.body().string();
                        Gson gson = new Gson();
                        mVideoEntity = gson.fromJson(result, VideoEntity.class);
                        if (mVideoEntity.getData().size() > 0) {
                            handler.sendEmptyMessage(1);
                        } else {
                            handler.sendEmptyMessage(0);
                        }
                    }
                });
            }
        }).start();
    }


    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    Toast.makeText(getActivity(), "抱歉，没有得到数据！", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    mVideoAdapter = new VideoAdapter(mVideoEntity.getData().get(0).getCatword_id(), getActivity());
                    mGridView.setAdapter(mVideoAdapter);
                    break;
            }
        }
    };
}
